package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.BannerDto;
import com.jilian.mydemo.common.dto.BannerListDto;
import com.jilian.mydemo.common.vo.BannerVo;
import com.jilian.mydemo.modul.main.ui.SplashActivity;
import com.jilian.mydemo.modul.main.ui.WelcomeActivity;
import com.jilian.mydemo.modul.user.activity.LoginActivity;
import com.jilian.mydemo.modul.user.activity.MyInfoActivity;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.UrlUtils;
import com.jilian.mydemo.utils.ZXingUtils;

import java.util.ArrayList;
import java.util.List;

public class MyQrCodeActivity extends BaseActivity {
    private ImageView ivQCode;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_myqrcode;
    }

    @Override
    public void initView() {
        setNormalTitle("二维码", v -> finish());
        ivQCode = (ImageView) findViewById(R.id.iv_q_code);
    }

    @Override
    public void initData() {
        getQcode();


    }
    private UserViewModel viewModel;
    private List<BannerDto> list;
    /**
     * 获取二维码
     */
    private void getQcode() {
        showLoadingDialog();
        BannerVo vo = new BannerVo();
        vo.setLocation(4);
        viewModel.getAppAdList(vo);
        viewModel.getBannerLiveData().observe(this, new Observer<BaseDto<BannerListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<BannerListDto> dto) {
                hideLoadingDialog();
                if (dto.isSuccess() && dto.getData() != null && EmptyUtils.isNotEmpty(dto.getData().getList())) {
                    list = new ArrayList<>();
                    list.addAll(dto.getData().getList());
                    Log.e(TAG, "url: "+list.get(0) );
                    Glide
                            .with(MyQrCodeActivity.this)
                            .load(list.get(0).getAdLogo())
                            .into(ivQCode);




//                    Bitmap bitmap = ZXingUtils.createQRImage(list.get(0).getAdLogo(), DisplayUtil.dip2px(MyQrCodeActivity.this,230),  DisplayUtil.dip2px(MyQrCodeActivity.this,230));
//                    ivQCode.setImageBitmap(bitmap);
                }
            }
        });
    }

    @Override
    public void initListener() {

    }
}
